package com.example.qixin0314;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.againstsky.verificationcode.VerificationCodeView;

import org.jetbrains.annotations.NotNull;

import cn.leancloud.LCUser;
import cn.leancloud.sms.LCSMS;
import cn.leancloud.sms.LCSMSOption;
import cn.leancloud.types.LCNull;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivity extends AppCompatActivity {

    private static final String TAG = RegisterActivity.class.getSimpleName();
    ImageView goPack;
    EditText edit_admin;
    EditText edit_yanzhengma;
    EditText edit_phone_psd;
    TextView send_phone_psd;
    EditText edit_pswds;
    Button btn_toregister;
    VerificationCodeView code_view;
    private LCSMSOption mOption;
    String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();//控件初始化
        Asend_phone_psd();
        goback();//返回
        Abtntoregister();//提交按钮点击事件
    }

    private void initView() {
        edit_admin=findViewById(R.id.edit_admin);
        edit_yanzhengma=findViewById(R.id.edit_yanzhengma);
        edit_phone_psd=findViewById(R.id.edit_phone_psd);
        send_phone_psd=findViewById(R.id.send_phone_psd);
        edit_pswds=findViewById(R.id.edit_pswds);
        btn_toregister=findViewById(R.id.btn_toregister);
        code_view=findViewById(R.id.code_view);
    }

    private void Asend_phone_psd(){
        send_phone_psd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = edit_admin.getText().toString();
                mOption = new LCSMSOption();
                mOption.setTtl(10);
                mOption.setApplicationName("qxswtSMS");
                mOption.setOperation("注册验证");
                mOption.setTemplateName("验证码");
                mOption.setSignatureName("验证码");
                LCSMS.requestSMSCodeInBackground(phone, mOption)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCNull>()
                        {
                            @Override
                            public void onSubscribe(@NotNull Disposable disposable)
                            {

                            }
                            @Override
                            public void onNext(@NotNull LCNull lcNull)
                            {
                                Toast.makeText(RegisterActivity.this, "短信发送成功", Toast.LENGTH_SHORT).show();
                            }
                            @Override
                            public void onError(@NotNull Throwable throwable)
                            {
                                Toast.makeText(RegisterActivity.this, "短信发送失败", Toast.LENGTH_SHORT).show();
                                Log.d(TAG, "error" + throwable.getMessage(), throwable);
                            }
                            @Override
                            public void onComplete()
                            {

                            }
                        });
            }
        });

    }

    private void Abtntoregister() {
        btn_toregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String imageNum = edit_yanzhengma.getText().toString();
                String phonePsd = edit_phone_psd.getText().toString();
                String pwds = edit_pswds.getText().toString();
                if (TextUtils.isEmpty(phone)){
                    Toast.makeText(RegisterActivity.this,
                            "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(imageNum)){
                    Toast.makeText(RegisterActivity.this,
                            "请输入图片验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(phonePsd)){
                    Toast.makeText(RegisterActivity.this,
                            "请输入手机验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pwds)){
                    Toast.makeText(RegisterActivity.this,
                            "请设置账户密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (phone.length()==11){
                    if (imageNum.equals(code_view.getVerificationCode())){
                        if (pwds.length()>=6){
                            LCSMS.verifySMSCodeInBackground(phonePsd, phone)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Observer<LCNull>()
                                    {
                                        @Override
                                        public void onSubscribe(@NotNull Disposable d)
                                        {

                                        }
                                        @Override
                                        public void onNext(@NotNull LCNull lcNull)
                                        {
                                            LCUser user=new LCUser();
                                            user.setUsername(phone);
                                            user.setPassword(pwds);
                                            user.setMobilePhoneNumber(phone);
                                            user.signUpInBackground()
                                                    .subscribeOn(Schedulers.io())
                                                    .observeOn(AndroidSchedulers.mainThread())
                                                    .subscribe(new Observer<LCUser>() {
                                                        @Override
                                                        public void onSubscribe(@NotNull Disposable d) { }

                                                        @Override
                                                        public void onNext(@NotNull LCUser lcUser) {
                                                            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                                                            Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                                                            intent.putExtra("phone",phone);
                                                            startActivity(intent);
                                                        }

                                                        @Override
                                                        public void onError(@NotNull Throwable e) {
                                                            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                                                        }
                                                        @Override
                                                        public void onComplete() {

                                                        }
                                                    });
                                        }
                                        @Override
                                        public void onError(@NotNull Throwable e)
                                        {
                                            Toast.makeText(RegisterActivity.this, "短信验证失败", Toast.LENGTH_SHORT).show();
                                            Log.e(TAG, "Result: Failed to verify the number", e);
                                        }
                                        @Override
                                        public void onComplete()
                                        {

                                        }
                                    });


                        }else {
                            Toast.makeText(RegisterActivity.this,
                                    "密码不能小于6位", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }else {
                        Toast.makeText(RegisterActivity.this,
                                "图片验证码错误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }else{
                    Toast.makeText(RegisterActivity.this,
                            "请输入正确手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }



    private void goback() {
        goPack=findViewById(R.id.go_back);
        goPack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.this.finish();
            }
        });
    }
}